package com.tmsps.ne4Weixin.api;

import java.util.HashMap;
import java.util.Map;

import com.tmsps.ne4Weixin.config.WxConfig;
import com.tmsps.ne4Weixin.utils.HttpClient;
import com.tmsps.ne4Weixin.utils.PaymentUtil;
import com.tmsps.ne4Weixin.utils.XmlHelper;

/**
 * 刷卡支付 API
 * 
 * @author 冯晓东
 *
 */
public class PaymentAPIScan extends BaseAPI {
	public PaymentAPIScan(WxConfig config) {
		super(config);
	}

	// 文档地址：https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
	private static String WXSCANPAYURL = "https://api.mch.weixin.qq.com/pay/micropay";

	/**
	 * 生成订单
	 * 
	 * @return
	 */
	public Map<String, String> scanQrcode(Map<String, String> params) {

		params.put("appid", config.getAppid());
		params.put("mch_id", config.getMch_id());

		params.put("nonce_str", System.currentTimeMillis() + "");
		String sign = PaymentUtil.createSign(params, config.getPayAPI());
		params.put("sign", sign);
		System.err.println(PaymentUtil.toXml(params));
		
		String body = HttpClient.postXML(WXSCANPAYURL, PaymentUtil.toXml(params));

		return XmlHelper.of(body).toMap();
	}

	public static void main(String[] args) {
		String appid = null;String secret = null;String encodingaeskey = null;boolean issafe=false; String token  = null; int expires_time = 7200;
		WxConfig wxConfig = new WxConfig(appid, secret, encodingaeskey, issafe, token);//根据业务逻辑自己设置

		PaymentAPIScan api = new PaymentAPIScan(wxConfig);
		Map<String, String> params = new HashMap<>();
		params.put("body", "image");
		params.put("out_trade_no", System.currentTimeMillis() + "");
		params.put("total_fee", "1");
		params.put("spbill_create_ip", "127.0.0.1");
		params.put("auth_code", "130221833949997081");

		Map<String, String> end = api.scanQrcode(params);
		System.err.println(end);

	}
}
